package org.example.read;

import org.example.pojo.BOM;
import org.example.pojo.JavaType;
import org.example.pojo.Member;
import org.example.pojo.Properties;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadBom {
    private static final String pubCla = "public class";
    private static final String SHARP = "#";


    public static void test01(String[] args) {
//        String fileName = "D:\\Study\\my-common-utils\\bom转excel\\NBUWXOM.bom";
        String fileName = "D:\\Study\\my-common-utils\\bom转excel\\NBUWXOM_zh_CN.voc";
//        readBom(fileName);
        readXomZhZc(fileName);
    }

    /**
     * 读取NBUWXOM_zh_CN.voc文件
     *
     * @param fileName
     * @return
     */
    public static List<JavaType> readXomZhZc(String fileName) {
        File file = new File(fileName);
        if (!file.exists()) {
            System.out.println("文件不存在!");
        }
        BufferedReader bufferedReader = null;
        FileReader fileReader = null;
        List<JavaType> javaTypeList = new ArrayList<>();
        JavaType javaType = new JavaType();
        String className = "";
        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                //# com.sinosoft.brms.nb.ind.AdviceProduct
                if (line.startsWith(SHARP)) {
                    if (!"".equals(className)) {
                        javaTypeList.add(javaType);
                        javaType = new JavaType();
                    }
                    className = line.split(SHARP)[1].trim();
                    javaType.setName(className);
                    System.out.println(className);
                }
                if (line.startsWith(className)) {
                    //com.sinosoft.brms.nb.ind.AdviceProduct#concept.label = 推荐主险
                    if (line.contains("concept.label")) {
                        String description = line.split("=")[1].trim();
                        javaType.setDescription(description);
                    }
                    //com.sinosoft.brms.nb.ind.AdviceProduct.amount#phrase.navigation = {this} 的{保额}
                    if (line.contains("phrase.navigation")) {
                        Properties properties = parsePhraseNavigation(line);
                        System.out.println(properties);
                        javaType.addProperties(properties);
                    }
                }
            }
            //最后一个JavaType
            if (!"".equals(className)) {
                javaTypeList.add(javaType);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeFileReader(fileReader);
            closeBufferedReader(bufferedReader);
        }
        return javaTypeList;
    }

    //com.sinosoft.brms.nb.ind.Applicant.addressIsVerif#phrase.navigation = {this} 现/常住地址验证(默认未验证)
    //com.sinosoft.brms.nb.ind.Applicant.agentCellerRepeatSum#phrase.navigation = {this} 的{相同联系方式业务员数}
    public static Properties parsePhraseNavigation(String msg) {
        String nameStr = msg.split("#")[0].trim();
        String chNameStr = msg.split("=")[1].trim();
        String phraseNavigation = "";
        String name = nameStr.substring(nameStr.lastIndexOf(".") + 1);
        System.out.println(nameStr);
        System.out.println(name);
        if (chNameStr.contains("{this}")) {
            chNameStr = chNameStr.replaceAll("\\{this\\}", "").trim();
        }
        System.out.println(chNameStr);
        if (chNameStr.contains("{")) {
            int startIndex2 = chNameStr.lastIndexOf("{") + 1;
            int endIndex2 = chNameStr.lastIndexOf("}");
            phraseNavigation = chNameStr.substring(startIndex2, endIndex2);
        } else {
            phraseNavigation = chNameStr;
        }
        Properties properties = new Properties();
        properties.setPhraseNavigation(phraseNavigation);
        properties.setName(name);


        System.out.println(properties);
        return properties;
    }

    public static void main(String[] args) {
        ReadBom.parsePhraseNavigation("com.sinosoft.brms.nb.ind.Applicant.addressIsVerif#phrase.navigation = {this} 现/常住地址验证(默认未验证)");
    }


    /**
     * 读NBUWXOM.bom文件
     *
     * @param fileName
     * @return
     */
    public static Map<String, JavaType> readBom(String fileName) {
        File file = new File(fileName);
        Map<String, JavaType> javaTypeMap = new HashMap<>();
        if (!file.exists()) {
            System.out.println("文件不存在!");
        }
        BufferedReader bufferedReader = null;
        FileReader fileReader = null;
        JavaType javaType = null;
        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.startsWith(pubCla)) {
                    String className = line.substring(pubCla.length()).trim();
                    javaType = new JavaType();
                    javaType.setName(className);
                } else if (line.startsWith("}")) {
                    javaTypeMap.put(javaType.getName(), javaType);
                } else if (line.contains("public") && line.endsWith(";")) {
                    if(line.contains("(") || line.contains("()")) {
                        continue;
                    }
                    Properties properties = new Properties();
                    String[] attribute = line.trim().split(" ");
                    int size = attribute.length;
//                    System.out.println(line+"--"+size);
                    if (size < 3) {
                        continue;
                    }
                    properties.setName(attribute[size - 1].replaceAll(";", "").trim());
                    properties.setStyle(attribute[size - 2]);
                    javaType.putProperties(properties);
                }
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeFileReader(fileReader);
            closeBufferedReader(bufferedReader);

        }
       /* for (JavaType ja : javaTypeList) {
            System.out.println(ja);
        }*/


        return javaTypeMap;
    }

    public static void closeFileReader(FileReader fileReader) {
        if (fileReader != null) {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void closeBufferedReader(BufferedReader bufferedReader) {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
